<template>
    <div class="product-detail-inventory">
        <p>
            Our real-time inventory keeps you informed of the availability of
            each product variant. Once a variant is marked as "Out of stock", we
            will replenish it within 5 business days. Variants marked as
            "unavailable" are not included in our inventory.
        </p>
        <ul>
            <li>In stock</li>
            <li>Out of stock</li>
            <li>
                <img src="@/assets/images/inventory-unavailable.svg" alt="">
                Unavailable
            </li>
        </ul>
        <div class="table-scroll">
            <table>
                <thead>
                    <tr>
                        <th></th>
                        <th v-for="item in inventoryData.sizes" :key="item" v-text="item"></th>
                    </tr>
                </thead>
                <tbody>
                    <template v-for="(item, key, index) in inventoryData.list">
                        <tr :key="index">
                            <td>
                                <p class="color-block" :style="'background-color:' + item[0].color_num"></p>
                                <span :title="key" v-text="key"></span>
                            </td>
                            <!-- quality >= -99 绿色 || <=-99 黄色 || 不存在或者-999 X -->
                            <td v-for="(child, index) in item" :key="child.size_name" :class="{
								stock: child.quality >= -99,
								'un-stock': child.quality < -99,
                                unavailable:(!child.quality && child.quality != 0) || child.quality <= -999}">
                                <img src="@/assets/images/inventory-unavailable.svg" alt="">
                            </td>
                        </tr>
                    </template>
                </tbody>
            </table>
        </div>

    </div>
</template>
<script>
export default {
    name: "DetailInventory",
    props: ["detail"],
    data () {
        return {};
    },
    computed: {
        inventoryData () {
            // 组数据
            let inventory = this.detail.inventory_list || [];
            inventory.sort((a, b) => {
                return a.size_sort - b.size_sort;
            });
            let list = {},
                sizes = [];
            inventory.map((item) => {
                sizes.push(item.size_name);
                if (list[item.color_name]) {
                    list[item.color_name].push(item);
                } else {
                    list[item.color_name] = [item];
                }
            });
            sizes = [...new Set(sizes)];
            return { list, sizes };
        },
    },
    methods: {},
};
</script>
<style scoped lang="scss">
.product-detail-inventory {
    width: 100%;
    font-family: Roboto;
    & > p {
        font-size: 14px;
        font-weight: 400;
        color: #555555;
        line-height: 26px;
    }
    & > ul {
        display: flex;
        flex-direction: row;
        align-content: center;
        margin: 28px -14px;
        & > li {
            padding-left: 32px;
            position: relative;
            font-size: 14px;
            font-family: Roboto-Medium, Roboto;
            font-weight: 500;
            color: #555555;
            height: 24px;
            margin: 0 14px;
            &:nth-child(1) {
                &::before {
                    content: '';
                    position: absolute;
                    box-sizing: border-box;
                    left: 0;
                    top: 50%;
                    transform: translateY(-50%);
                    width: 24px;
                    height: 24px;
                    background: #bde7a7;
                    border-radius: 12px;
                    border: 1px solid rgba(0, 0, 0, 0.14);
                }
            }
            &:nth-child(2) {
                &::before {
                    content: '';
                    position: absolute;
                    box-sizing: border-box;
                    left: 0;
                    top: 50%;
                    transform: translateY(-50%);
                    width: 24px;
                    height: 24px;
                    background: #ebe8d8;
                    border-radius: 12px;
                    border: 1px solid rgba(0, 0, 0, 0.14);
                }
            }
            &:nth-child(3) {
                &::before {
                    content: '';
                    position: absolute;
                    box-sizing: border-box;
                    left: 0;
                    top: 50%;
                    transform: translateY(-50%);
                    width: 24px;
                    height: 24px;
                    background: #ffffff;
                    border-radius: 12px;
                    border: 1px solid rgba(0, 0, 0, 0.14);
                }
                img {
                    position: absolute;
                    left: 4px;
                    top: 50%;
                    transform: translateY(-50%);
                    width: 16px;
                    height: 16px;
                }
            }
        }
    }
    .table-scroll {
        width: 100%;
        overflow-x: auto;
        margin-bottom: 82px;
        table {
            margin-bottom: 8px;
            min-width: 800px;
            border: 1px solid #bdbdbd;
            overflow: auto;
            th,
            td {
                position: relative;
                width: 80px;
                height: 44px;
                font-size: 14px;
                font-weight: 400;
                color: #000000;
                line-height: 44px;
                white-space: nowrap;
                text-overflow: ellipsis;
                overflow: hidden;
                box-sizing: border-box;
            }
            th {
                text-align: center;
                &:nth-child(1) {
                    border-right: 1px solid #bdbdbd;
                }
                &:not(:last-child, :first-child) {
                    &::after {
                        content: '';
                        position: absolute;
                        right: 0;
                        top: 50%;
                        transform: translateY(-50%);
                        width: 1px;
                        height: 12px;
                        background: #bdbdbd;
                    }
                }
            }
            tbody {
                tr {
                    // border-top: 1px solid transparent;
                    td {
                        text-align: left;
                        &:first-child {
                            border-top: 1px solid #bdbdbd;
                            border-right: 1px solid #bdbdbd;
                            padding: 0 12px;
                            max-width: 160px;
                        }
                        &:not(:first-child) {
                            border-top: 1px solid rgba(#000000, 0.13);
                        }
                        &:not(:first-child, :nth-child(2)) {
                            border-left: 1px solid rgba(#000000, 0.13);
                        }
                        &:not(:first-child) {
                            width: 80px;
                            text-align: center;
                        }
                        img {
                            position: absolute;
                            top: 50%;
                            left: 50%;
                            transform: translate(-50%, -50%);
                            opacity: 0;
                            width: 16px;
                            height: 16px;
                        }
                        &.stock {
                            border-color: #a6cb93 !important;
                            background: #bde7a7;
                        }
                        &.un-stock {
                            border-color: #d1cec1 !important;
                            background: #ebe8d8;
                        }
                        &.unavailable {
                            background: transparent;
                            & > img {
                                opacity: 1;
                            }
                        }
                    }
                }
            }
            .color-block {
                display: inline-block;
                width: 14px;
                height: 14px;
                border: 1px solid rgba(0, 0, 0, 0.2);
                margin-right: 6px;
                margin-bottom: -2px;
            }
        }
    }
}

@media (max-width: 960px) {
    .product-detail-inventory table tbody tr td:first-child {
        max-width: 120px;
    }
    .product-detail-inventory > ul {
        max-width: 360px;
        margin: 20px 0;
        align-items: center;
        justify-content: space-between;
        li {
            margin: 0;
        }
    }
    .table-scroll {
    }
}
</style>
